package com.example.rabbitmqdemo.simple_queue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

public class Send {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("42.194.182.59");
        connectionFactory.setUsername("root");
        connectionFactory.setPassword("Albb123.");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPort(5672);

        try(Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel()){
            /**
             * queue – the name of the queue
             * 队列名称
             * durable – true if we are declaring a durable queue (the queue will survive a server restart)
             * 是否持久化
             * exclusive – true if we are declaring an exclusive queue (restricted to this connection)
             * 是否独占：只能有一个消费者监听队列；当connection关闭是否删除队列，一般是false，发布订阅是独占
             * autoDelete – true if we are declaring an autodelete queue (server will delete it when no longer in use)
             * 没有消费者的时候是否自动删除队列
             * arguments – other properties (construction arguments) for the queue
             */
            channel.queueDeclare("hello", true,false,false,null);
            String message = "hello world!!!!!!!!!!!!";
            /**
             * 参数说明：
             * 交换机名称：不写则是默认的交换机，那路由健需要和队列名称一样才可以被路由，
             * 路由健名称
             * 配置信息
             * 发送的消息数据：字节数组
             */
            channel.basicPublish("","hello",null,message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
